package GameStates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.me.mygdxgame.GameObjects.Player;
import com.me.mygdxgame.gamelogic.GameLogic;
import com.me.mygdxgame.gamelogic.GameLogicListener;
import com.me.mygdxgame.renderer.Context;
import com.me.mygdxgame.renderer.ContextManager;
import com.me.mygdxgame.renderer.Renderer;
import com.me.mygdxgame.renderer.RendererGL10;
import com.me.mygdxgame.renderer.RendererGL20;

public class GameState extends BaseState implements GameLogicListener
{
	private final GameLogic game;
	
	private final Renderer renderer;
	private Context context;
	
	private Player[] player;
	
	
	public GameState()
	{
		game = new GameLogic();
		game.listener = this;
		
		if(Gdx.graphics.isGL20Available())
		{
			Gdx.app.log("Renderer", "Created GL20 renderer");
			renderer = new RendererGL20();
		}
		else
		{
			Gdx.app.log("Renderer", "Created GL10 renderer");
			renderer = new RendererGL10();
		}
		
		ShaderProgram temp = new ShaderProgram(Gdx.files.internal("shaders/basic_vert.glsl").readString(), Gdx.files.internal("shaders/basic_frag.glsl").readString());
		if(temp.isCompiled() == false)
		{
			Gdx.app.log("ShaderError", temp.getLog());
//			System.exit(0);
		}
		
		context = new Context(temp);
		
		ContextManager.getInstance().addContext(context);
		
		player = new Player[10];
		
		for(int i = 0; i < 10; i++)
		{
			player[i] = new Player();
			
			context.addMesh(player[i].obj);
		}

		player[0].obj.translate(4, -5, -16);
		player[1].obj.translate(2, 0, -4);
		player[2].obj.translate(-1, -2, -14);
		player[3].obj.translate(-3, 2, -15);
		player[4].obj.translate(-2, 4, -16);
		player[5].obj.translate(-10, -9, -40);
		player[6].obj.translate(10, 9, -40);
		player[7].obj.translate(-1, 2, -14);
		player[8].obj.translate(1, 0, -11);
		player[9].obj.translate(5, 5, -19);
		
	}
	
	@Override
	public void dispose()
	{
		Gdx.app.log(GameState.class.getSimpleName(), "disposing");
		renderer.dispose();
	}


	@Override
	public void pause()
	{
		// TODO Auto-generated method stub
		
	}


	@Override
	public void PowerUp()
	{
		// TODO Auto-generated method stub
		
	}


	@Override
	public void Winner()
	{
		// TODO Auto-generated method stub
		
	}


	@Override
	public void update(float dt)
	{
		game.update(dt);
		
		for(int i = 0; i < 10; i++)
		{
			player[i].obj.rotate(30 * dt, 1, -1, 0);
		}
		
	}


	@Override
	public void draw(float dt)
	{
		renderer.render(game, dt);
	}


	@Override
	public boolean isDone()
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void input(float dt)
	{
		// TODO Auto-generated method stub
		
	}
	
	
}
